package com.example.nationmobile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;




import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;




public class ListAdapter extends BaseAdapter {
    
	
    private Activity activity;
    private static LayoutInflater inflater=null;
  //  public ImageLoader imageLoader; 
  
    ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();
    
    
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	 private ImageLoader imageLoader = ImageLoader.getInstance();
	 private DisplayImageOptions options;
	 
	 
	 
    
    public ListAdapter(Activity a,ArrayList al ) {
    	
    	activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        MyArrList=al;
       
    
    
        options = new DisplayImageOptions.Builder()
		.showStubImage(R.drawable.thumb)
		.showImageForEmptyUri(R.drawable.thumb)
		.showImageOnFail(R.drawable.thumb)
		.cacheInMemory()
		.cacheOnDisc()
		.displayer(new RoundedBitmapDisplayer(0))
		.build();
	initImageLoader(activity.getApplicationContext());
    
    }
    
    private class ViewHolder {
		public TextView title,description;
		public ImageView thumbnail;
	}


    @Override
	public void notifyDataSetInvalidated() {
		// TODO Auto-generated method stub
    	
    	Log.e("evt", "==========notifyDataSetInvalidated()=============");
    	
		super.notifyDataSetInvalidated();
	}
    @Override
	public int getCount() {
        return MyArrList.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
      
    	View vi=convertView;
    	final ViewHolder holder;
    	
        if(convertView==null)
        {
	            vi = inflater.inflate(R.layout.main_listview_item, null);
	        holder = new ViewHolder();
			
			
		    holder.thumbnail = (ImageView)vi.findViewById(R.id.img_list);
		    holder.title = (TextView)vi.findViewById(R.id.txt_title);
		    holder.description= (TextView)vi.findViewById(R.id.txt_detail);
			
			vi.setTag(holder);	
	 		AnimateFirstDisplayListener.displayedImages.clear();
	}else 
    {
	
    		holder = (ViewHolder)vi.getTag();
    }
            
        
        
        
        //=================================SetData===================================
      
        if(Control.current_tab==0)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail0.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
			try {holder.title.setText(Control.List_title0.get(position));}catch (Exception e) {e.printStackTrace();}
			try {holder.description.setText(Html.fromHtml(Control.List_detail0.get(position)));}catch (Exception e) {e.printStackTrace();}
        	
        }
        if(Control.current_tab==1)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail1.get(position+1), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
			try {holder.title.setText(Control.List_title1.get(position+1));}catch (Exception e) {e.printStackTrace();}
			try {holder.description.setText(Html.fromHtml(Control.List_detail1.get(position+1)));}catch (Exception e) {e.printStackTrace();}
        	
        }
        else if(Control.current_tab==2)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail2.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title2.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail2.get(position)));}catch (Exception e) {e.printStackTrace();}
    		
        }
        else if(Control.current_tab==3)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail3.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title3.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail3.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==4)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail4.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title4.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail4.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==5)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail5.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title5.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail5.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==6)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail6.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title6.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail6.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==7)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail7.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title7.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail7.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==8)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail8.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title8.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail8.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==9)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail9.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title9.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail9.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==10)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail10.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title10.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail10.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        if(Control.current_tab==11)
        {
        	try {imageLoader.displayImage(Control.List_thumbnail11.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
			try {holder.title.setText(Control.List_title11.get(position));}catch (Exception e) {e.printStackTrace();}
			try {holder.description.setText(Html.fromHtml(Control.List_detail11.get(position)));}catch (Exception e) {e.printStackTrace();}
        }
        else if(Control.current_tab==12)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail12.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title12.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail12.get(position)));}catch (Exception e) {e.printStackTrace();}
    		
        }
        else if(Control.current_tab==13)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail13.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title13.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail13.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==14)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail14.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title14.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail14.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==15)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail15.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title15.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail15.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==16)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail16.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title16.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail16.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==17)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail17.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title17.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail17.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==18)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail18.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title18.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail18.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==19)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail19.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title19.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail19.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==20)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail20.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title20.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail20.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==21)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail21.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title21.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail21.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==22)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail22.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title22.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail22.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==23)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail23.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title23.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail23.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
       /* else if(Control.current_tab==24)
        {
        	try {imageLoader.displayImage(Control.List_thumbnail_Opinoin.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title_Opinoin.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail_Opinoin.get(position)));}catch (Exception e) {e.printStackTrace();}
        	
        }*/
        
        else if(Control.current_tab==24)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail24.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title24.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail24.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        
        else if(Control.current_tab==25)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail25.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title25.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail25.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
        else if(Control.current_tab==26)
        {
        	
        	try {imageLoader.displayImage(Control.List_thumbnail26.get(position), holder.thumbnail, options, animateFirstListener);}catch (Exception e) {e.printStackTrace();}
    		try {holder.title.setText(Control.List_title26.get(position));}catch (Exception e) {e.printStackTrace();}
    		try {holder.description.setText(Html.fromHtml(Control.List_detail26.get(position)));}catch (Exception e) {e.printStackTrace();}
            
        }
       
	
	
	return vi;
    }
    
    
    public static void initImageLoader(Context context) {
  		// This configuration tuning is custom. You can tune every option, you may tune some of them, 
  		// or you can create default configuration by
  		//  ImageLoaderConfiguration.createDefault(this);
  		// method.
  		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		  		.memoryCacheExtraOptions(600, 800) // default = device screen dimensions
		        .threadPoolSize(3) // default
		        .threadPriority(Thread.NORM_PRIORITY - 1) // default
		        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
		        .denyCacheImageMultipleSizesInMemory()
		        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
		        .memoryCacheSize(2 * 1024 * 1024)
		        .discCacheSize(50 * 1024 * 1024)
		        .discCacheFileCount(100)
		        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
		        .imageDownloader(new BaseImageDownloader(context)) // default
		        .imageDecoder(new BaseImageDecoder()) // default
		        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
		        .build();
  		// Initialize ImageLoader with configuration.
  		ImageLoader.getInstance().init(config);
  	}
  	
    
  	@SuppressWarnings("unused")
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
    
    
}